<?php
class Blog extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function get($intBlogId) {
		try {
			$strKey 	= BACKEND_BLOG_GET . $intBlogId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call blogs_get(?)');
				$this->bindParam(1, $intBlogId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, BACKEND_BLOG_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Blog::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intStatus, $intOption, $strKey, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call blogs_getall_admin(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intOption, PDO::PARAM_STR, 50);
			$this->bindParam(3, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(4, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(5, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(6, $intOff, PDO::PARAM_INT, 4);
			$this->bindParam(7, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Blog::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function updateStatus($intAdminId, $intBlogId, $intStatus, $intPerm, $intUserId=0) {
		try {				
			$this->prepare('call blogs_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intBlogId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteBlogById($intBlogId);
				$this->clear->deleteBlog($intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Blog::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intAdminId, $intUserId, $intBlogId) {
		try {			
			$this->prepare('call blogs_delete_admin(?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intBlogId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg(0,$strImage);
				}
				$this->clear->deleteBlogById($intBlogId);
				$this->clear->deleteBlog($intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend_Blog::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImg($intBlogId, $strImg='') {
		if($strImg == '') {
			$arrRow = $this->get($intBlogId);
			if(isset($arrRow) && $arrRow) {
				if(isset($arrRow['image']) && $arrRow['image'] != '') {
					if(file_exists(DIR_BLOG_FULL . $arrRow['image']))
						@unlink(DIR_BLOG_FULL . $arrRow['image']);
					if(file_exists(DIR_BLOG_THUMB . $arrRow['image']))
						@unlink(DIR_BLOG_THUMB . $arrRow['image']);
					if(file_exists(DIR_BLOG_SMALL . $arrRow['image']))
						@unlink(DIR_BLOG_SMALL . $arrRow['image']);
				}
			}
		} else {
			if(file_exists(DIR_BLOG_FULL . $strImg))
				@unlink(DIR_BLOG_FULL . $strImg);
			if(file_exists(DIR_BLOG_THUMB . $strImg))
				@unlink(DIR_BLOG_THUMB . $strImg);
			if(file_exists(DIR_BLOG_SMALL . $strImg))
				@unlink(DIR_BLOG_SMALL . $strImg);
		}
	}
}
?>